package eu.jakubiak.patches.google.ajaxapi.translation
{
	import be.boulevart.google.ajaxapi.translation.GoogleTranslation;
	import be.boulevart.google.ajaxapi.translation.data.GoogleLanguageDetectionResult;
	import be.boulevart.google.ajaxapi.translation.data.GoogleTranslationResult;
	import be.boulevart.google.events.GoogleAPIErrorEvent;
	import be.boulevart.google.events.GoogleApiEvent;
	
	import com.adobe.serialization.json.JSON;
	
	import eu.jakubiak.patches.google.ajaxapi.helpers.URLVariablesWithKey;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class GoogleTranslation extends be.boulevart.google.ajaxapi.translation.GoogleTranslation
	{
		
		private var _orig : String
		private var _from : String
		private var _to : String
		
		override public function translate(textToTranslate : String,langFromCode : String,langToCode : String) : void {
			var serviceURL : String = "http://ajax.googleapis.com/ajax/services/language/translate"
			var query : String = ""
			
			_orig = textToTranslate
			_from = langFromCode
			_to = langToCode
			var request : URLRequest = new URLRequest(serviceURL)
				
			request.data = new URLVariablesWithKey();
			request.data.v = "1.0";
			request.data.q = textToTranslate;
			request.data.langpair = langFromCode + "|" + langToCode;
				
			var loader : URLLoader = new URLLoader()
			loader.addEventListener(Event.COMPLETE , onResponse)
			loader.addEventListener(IOErrorEvent.IO_ERROR , onIOError); 
			loader.load(request)
		}
		
		override public function detectLanguage(text : String) : void {
			var serviceURL : String = "http://ajax.googleapis.com/ajax/services/language/detect"
			
			var request : URLRequest = new URLRequest(serviceURL);
			request.data = new URLVariablesWithKey();
			request.data.v = "1.0";
			request.data.q = text;
				
			var loader : URLLoader = new URLLoader()
			loader.addEventListener(Event .COMPLETE , onDetectionResponse)
			loader.addEventListener(IOErrorEvent.IO_ERROR , onIOError); 
			loader.load(request)
		}
		
		private function onResponse(event : Event) : void {
			var json : Object = JSON.decode("" + event.target.data);
			var results : String = json.responseData ? json.responseData.translatedText as String : "";
			
			var t : GoogleTranslationResult = new GoogleTranslationResult()
			t.result = results
			t.from = _from
			t.to = _to
			t.orig = _orig
			
			dispatchEvent(new GoogleApiEvent(GoogleApiEvent.TRANSLATION_RESULT , t))
		}
		
		private function onDetectionResponse(event : Event) : void {
			var json : Object = JSON.decode("" + event.target.data);
			if(json.responseData != null) {
				var language : String = json.responseData.language as String
				var isReliable : Boolean = json.responseData.isReliable as Boolean
				var confidence : Number = json.responseData.confidence as Number 
				
				var detect : GoogleLanguageDetectionResult = new GoogleLanguageDetectionResult()
				detect.language = language
				detect.isReliable = isReliable
				detect.confidence = confidence
				
				dispatchEvent(new GoogleApiEvent(GoogleApiEvent.TRANSLATION_LANGUAGE_DETECTION_RESULT , detect))
			}else {
				dispatchEvent(new GoogleAPIErrorEvent(GoogleAPIErrorEvent.API_ERROR , json.responseDetails as String , json.responseStatus as int))     
			}
		}
		
		private function onIOError(event : IOErrorEvent) : void {  
			dispatchEvent(new GoogleApiEvent(GoogleApiEvent.ON_ERROR , "IOERROR: " + event.text))
		} 
		
		
	}
}